package com.chaodev.base.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.MemberDto;
import com.chaodev.base.model.Member;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.service.MemberService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/22 11:16
 **/
@RestController
@RequestMapping("member")
@Api("会员管理")
public class MemberController {
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;
    @Autowired
    private MemberService memberService;
    /**
     * 获取会员表列表分页
     */
    @ApiOperation(value = "获取会员表列表分页", notes = "获取会员表列表分页")
    @PostMapping(value = {"/list"})
    public Result list(@RequestBody MemberDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Member member = new Member();
            BeanUtil.copyProperties(dto,member);
            if (null == dto.getPage()) {
                dto.setPage(1);
            }
            if (null == dto.getRows()) {
                dto.setRows(20);
            }
            PageInfo<Member> memberList = memberService.queryPageList(dto.getPage(),dto.getRows(),member);
            result.setCode(0);
            result.setMsg("获取成功");
            result.setData(memberList);
            logService.logAdd(null, "获取会员表列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取会员表列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 获取会员表对象
     */
    @ApiOperation(value = "获取会员表对象", notes = "获取会员表对象")
    @GetMapping(value = {"/getDataById"})
    public Result findById(String id,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Member dataById = memberService.findDataById(id);
            result.setCode(0);
            result.setData(dataById);
            logService.logAdd(null, "获取会员表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取会员表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存会员表对象
     */
    @ApiOperation(value = "保存会员表对象", notes = "保存会员表对象")
    @PostMapping(value = {"/save"})
    public Result save(@RequestBody Member member,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            memberService.saveData(member);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(member);
            logService.logAdd(null, "保存会员表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存会员表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除会员表对象
     */
    @ApiOperation(value = "删除会员表对象", notes = "删除会员表对象")
    @GetMapping(value = {"/del"})
    public Result delete(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            memberService.deleteDataById(id);
            result.setCode(0);
            logService.logAdd(null, "删除会员表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除会员表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }
}
